<template>
  <div>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="[16,16]">
            <a-col span="12">
              <a-form-item label="代理名称">
                {{ $route.query.name }}
              </a-form-item>
            </a-col>
            <a-col span="12">
              <a-form-item label="代理手机号">
                {{ $route.query.phone }}
              </a-form-item>
            </a-col>
            <a-col span="8">
              <a-form-item label="时间">
                <a-range-picker :allowClear="true" ref="createdAtRangePicker" @change="createdAtRangePickerOnChange"/>
              </a-form-item>
            </a-col>
            <!--            <a-col :md="8" :sm="24">-->
            <!--              <a-form-item label="订单号">-->
            <!--                <a-input v-model="queryParam.name" placeholder=""/>-->
            <!--              </a-form-item>-->
            <!--            </a-col>-->
            <a-col span="12">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="handleReset">重置</a-button>
                <!--                <a-button style="margin-left: 8px" >导出</a-button>-->
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :scroll="{ x: 1300 }"
        :data="loadData"
        showPagination="auto"
      />
    </a-card>
  </div>
</template>

<script>
  import { STable } from '@/components'
  import PartnersForm from '@/views/proxy/partner/Form'
  import { getAgentProfitRunningBill } from '@/api/agent'
  import { formatAmount } from '@/utils/util'
  import { mapGetters } from 'vuex'

  export default {
    name: 'List',
    components: {
      PartnersForm,
      STable
    },
    computed: {
      ...mapGetters(['revenuesType'])
    },
    data () {
      return {
        // 查询参数
        queryParam: {
          agent_id: this.$route.params.id
        },
        // 表头
        columns: [
          {
            title: '序号',
            dataIndex: 'id'
          },
          {
            title: '时间',
            dataIndex: 'created_at'
          },
          {
            title: '流水类型',
            dataIndex: 'type',
            customRender: type => this.revenuesType[type].text
          },
          {
            title: '金额',
            customRender: item => formatAmount(item.amount * (item.revenues_type === 2 ? -1 : 1)) // revenues_type = 2 为支出
          },
          {
            title: '订单号',
            dataIndex: 'source.numbering'
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return getAgentProfitRunningBill(Object.assign(parameter, this.queryParam)).then(res => {
            return res
          })
        },
        selectedRowKeys: [],
        selectedRows: []
      }
    },
    created () {

    },
    methods: {
      createdAtRangePickerOnChange (date, dateString) {
        this.queryParam.created_at_begin = dateString[0]
        this.queryParam.created_at_end = dateString[1]
      },
      handleReset () {
        this.queryParam = {}
        this.$refs.createdAtRangePicker.$refs.picker.clearSelection(new MouseEvent('click'))
      }
    }
  }
</script>
